package com.k.kpp.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author kivil
 */
public class JobUtils {
    private static Scheduler scheduler;


    /**
     * create job
     * @param jobName  name
     * @param jobGroup group
     * @param jobClass class
     * @param jobCronExpression '0/5 * * * * ?'
     * @throws SchedulerException e
     */
    private static void createJob(String jobName, String jobGroup, Class<? extends Job> jobClass, String jobCronExpression) throws SchedulerException {
        // 任务
        JobDetail jobDetail = JobBuilder.newJob(jobClass)
                .withIdentity(getJobKey(jobName, jobGroup))
                .build();

        // 触发器
        CronTrigger jobTrigger = TriggerBuilder.newTrigger()
                .withIdentity(getTriggerKey(jobName, jobGroup))
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(jobCronExpression)
                        .withMisfireHandlingInstructionDoNothing())
                .build();

        // 调度
        scheduler.scheduleJob(jobDetail, jobTrigger);
    }

    private static void deleteJob(String jobName, String jobGroup) throws SchedulerException {
        scheduler.deleteJob(getJobKey(jobName, jobGroup));
    }
    private static boolean isExistsJob(String jobName,String jobGroup) throws SchedulerException {
        return scheduler.checkExists(getJobKey(jobName, jobGroup));
    }
    private static void startJob(String jobName,String jobGroup) throws SchedulerException {
        scheduler.triggerJob(getJobKey(jobName, jobGroup));
    }
    private static void stopJob(String jobName,String jobGroup) throws SchedulerException {
        scheduler.pauseJob(getJobKey(jobName, jobGroup));
    }

    private static JobKey getJobKey(String name, String group){
        return JobKey.jobKey(name, group);
    }
    private static TriggerKey getTriggerKey(String name,String group){
        return TriggerKey.triggerKey(name, group);
    }

    /**
     *  create job
     * @param jobName name
     * @param jobGroup group
     * @param jobClass target
     * @param jobCronExpression cron
     * @param jobStatus status 1：暂停任务
     * @throws SchedulerException e
     */
    public static void bizCreateJob(String jobName,String jobGroup,Class<? extends Job> jobClass,String jobCronExpression,int jobStatus) throws SchedulerException{
        if (isExistsJob(jobName,jobGroup)) {
            deleteJob(jobName,jobGroup);
        }
        createJob(jobName,jobGroup,jobClass,jobCronExpression);
        if(jobStatus==1){
            stopJob(jobName,jobGroup);
        }
    }

    /**
     * delete job
     * @param jobName name
     * @param jobGroup group
     * @throws SchedulerException e
     */
    public static void bizDeleteJob(String jobName,String jobGroup) throws SchedulerException {
        if(isExistsJob(jobName,jobGroup)){
            deleteJob(jobName,jobGroup);
        }
    }

    /**
     * update job
     * @param name name
     * @param group group
     * @param jobClass target
     * @param jobCronExpression cronExpression
     * @throws SchedulerException e
     */
    public static void bizUpdateJob(String name,String group,Class<? extends Job> jobClass,String jobCronExpression) throws SchedulerException {
        if (isExistsJob(name,group)){
            // 防止创建时存在数据问题 先移除，然后在执行创建操作
            deleteJob(name,group);
        }
        createJob(name,group,jobClass,jobCronExpression);
    }
    public static void bizStartJob(String name,String group) throws SchedulerException {
        if(isExistsJob(name,group)){
            System.out.println("=====found job");
            startJob(name,group);
        }else{
            System.out.println("=====not fount job");
        }
    }

    public static void main(String[] args) {
        try {
            JobUtils.scheduler = StdSchedulerFactory.getDefaultScheduler();
            // 创建任务
            JobUtils.bizCreateJob("job1","jobGroup1",JobOne.class,"0/5 * * * * ?",0);
            // 创建暂停的任务
            JobUtils.bizCreateJob("job1","jobGroup1",JobOne.class,"0/5 * * * * ?",1);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

